<?php if (count($templates) > 0){?>

<div id="notification" class="nNote nSuccess hideit" onclick="$('#notification').hide('slow')" style="margin-top: 40px;display: none">
				                 <p><strong>SUCCESS: </strong>Search Definition Saved</p>
</div>
    <div class="widget" >
    <div class="head"><h5 class="iList">Search Configuration for <?php echo $directory->getName()?></h5></div>
     <form action="<?php echo url_for('search_configuration/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" id="valid" class="mainForm" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
    	
     		<input type="hidden" name="directory_id" id="directory_id" value="<?php echo $directory->getId()?>">
    	
     
		<div id="templates_involved_levels">
			<?php include_partial("template_levels_container", array("templates" => $templates,  "categories" => $categories, "levelIds" => $levelIds, 'selected_template_id' => $selected_template_id, 'inclusive_categories' => $inclusive_categories, 'selected_directory' => $selected_directory))?>
		</div>
     	
     	<div class="fix"></div>
     	

     	
        <input type="button" value="Save" class="greyishBtn submitForm" id="save" />
        
     	<?php if (!$form->getObject()->isNew()){
     		?>
     		<input type="button" value="Create New" class="greyishBtn submitForm" id="create_new" />
     	<?php 
     	} ?>
     	        
        <div class="fix"></div>
     </form>
     
    </div> 
    
    <div id="error_msg" class="error_msg" style="clear:both; display: none; cursor:pointer;" onclick="$(this).hide('slow')">
		<div id="msg_text" ></div>
	</div>
     
<script type="text/javascript">

function validate(){
	var valid = true;
	var error_msg = "";
	if ($("#search_title").attr("value") == ""){
		valid = false;
		error_msg = "Search title required";
	}	
	else if (valid && $("#search_button_caption").attr("value") == ""){
		valid = false;
		error_msg = "Search button caption required";
	}		
	if (!valid){
		$('#msg_text').html(error_msg);
		$('#error_msg').show('fast');
	}
	
	return valid;			
}

$("#save").click(function (event){
    submitForm();
})

$("form#valid").submit(function (event){
    event.preventDefault();
    submitForm();
})




		function submitForm(){
		    var str=$('form#valid').serialize();
		
		    $.ajax({type: 'POST', url:' <?php echo url_for('search_configuration/'.($form->getObject()->isNew() ? 'create' :'update').
		    (!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : ''))?>', script:true, cache:false, data:  str, 
		    beforeSend: function(){
		        if (!validate()){
					return false;
		        }
		         },
		
		            success: function(data) {
		                 $('#templates_involved_levels').html(data);
		               	 
		             } });
		}

		
		$('form').jqTransform({imgPath:'../images/forms'});

		function loadTemplatesData(){

			var php_url = '<?php echo url_for('search_configuration/loadTemplateData?directory_id=')?>';
			var directory_id = document.getElementById('directory_id').value;
			php_url = php_url.concat(directory_id);	
			
			$.ajax({type: 'POST', url:php_url, script:true, cache:false, 
				success: function(data) { 
					 $('#templates_involved_levels').html(data);

					 
					} });
			
		}

		
</script>

<style>
	.jqTransformCheckboxWrapper{
	 	
	    margin-top: 0px !important;
	   
	}
	
	.jstree a .jstree-icon { display:none !important; }
	
	 .tree_caption{
	    margin-left: 3px;
	    position: relative;
	    top: -2px;	 	
	 }
</style>
  
  <?php }
  else{
  ?>
  
	<div style="margin-top:40px">
	<i>There are no templates created</i>
	</div>  
  
  <?php 
    }
  ?>
